
import java.awt.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

/**
 * The Assembler class - Converting "asm" to "bin" (Intermediate Format).
 * @author Hemant and Sandeep
 *
 */

public class test7 {
	 public static String fillString(char filler, int count){
	        char[] Cary = new char[count];
	        while(count>0){ count--;  Cary[count] = filler ;   }
	        return new String(Cary);
	        }
	 @SuppressWarnings("unchecked")
	public static void main(String args[]) throws IOException{
		 
		Vector<Vector<String>> vector1 = new Vector<Vector<String>>();
		Vector<String> vector2 = new Vector<String>();
		Vector<String> vector3 ;
		int m=0;
		String[] label = new String [50] ;
		BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\User\\Desktop\\CA\\assemblerout.txt"));
		//String[] Str1 = new String [10];
		Scanner s = null;
		String[] jumpaddr = new String[50];
		// Forming the Key Value pairs by using a Hasp-map, we can also use a Hash-table here!!      
        Hashtable<String, String> ht = new Hashtable<String,String>();
       
    // forming the Key value pairs for the Instructions both R-type and I-type
   
    ht.put("add","100000");
    ht.put("sub","100010");
    ht.put("load","100011");
    ht.put("Store","100100");
    ht.put("bez","100110");
    ht.put("addi","100111");
    // forming the key value pairs for the registers
   
    ht.put("r0", "00000");
    ht.put("r1", "00001");
    ht.put("r2", "00010");
    ht.put("r3", "00011");
    ht.put("r4", "00100");
    ht.put("r5", "00101");
    ht.put("r6", "00110");
    ht.put("r7", "00111");
    ht.put("r8", "01000");
    ht.put("r9", "01001");
    ht.put("r10", "01010");
    ht.put("r11", "01011");
    ht.put("r12", "01100");
    ht.put("r13", "01101");
    ht.put("r14", "01110");
    ht.put("r15", "01111");
    ht.put("r16", "10000");
    ht.put("r17", "10001");
    ht.put("r18", "10010");
    ht.put("r19", "10011");
    ht.put("r20", "10100");
    ht.put("r21", "10101");
    ht.put("r22", "10110");
    ht.put("r23", "10111");
    ht.put("r24", "11000");
    ht.put("r25", "11001");
    ht.put("r26", "11010");
    ht.put("r27", "11011");
    ht.put("r28", "11100");
    ht.put("r29", "11101");
    ht.put("r30", "11110");
    ht.put("r31", "11111");
		File file = new File("C:\\Users\\User\\Desktop\\CA\\example.txt");
		Scanner scanner = null;
		try {
			scanner = new Scanner(file);
		} catch(FileNotFoundException e) {
			System.out.println("file not found: " + e.getMessage());
		}
		while(scanner.hasNextLine()) {
			String line = scanner.nextLine();
			vector2.addElement(line);
			System.out.println(vector2);
		}
		scanner.close();

		System.out.println("Vector2 Size " + vector2.size());

		for(int j = 0; j < vector2.size(); j++)
		{ 
			  
			vector3 = new Vector() ;
			s = new Scanner(vector2.get(j));
			s.useDelimiter("\\s*,*\\s");
						while(s.hasNext())
			{
				vector3.addElement(s.next());
				
			}
			
			vector1.addElement(vector3);
		}
		


        //System.out.println("what is " + vector1.get(5).size());
		System.out.println("Size" + vector1.size());
		for(int j=0;j<vector1.size();j++)
		{ int k=0;

		while((vector1.get(j)).size()>k)

		{  	System.out.println((vector1.get(j)).get(k));
		k++;
		}

		}
		for (int n=0; n<vector1.size(); n++)
	       { int k=0;
	       String bez = "bez" ;
	         if(((vector1.get(n)).get(0)).equals(bez))
	          {
	         
	            m++;
	           while((vector1.get(n)).size()>k)
	             { k++; }
	           label[m] = (vector1.get(n)).get(k-1);
	           	           }
	        }
		for (int k=1; k<=m; k++)    
        {
         for (int n=0; n<vector1.size(); n++)
         	
          {    int j=0; 
               String lab = label[k] + ":" ;
                                           
               if((vector1.get(n)).get(j).equals(lab))
              
                 {  
                  
                   String temp = (Integer.toBinaryString(n)) ;
                  
                    jumpaddr[k] = fillString('0',8-temp.length()) + temp;
                         
                                     }
                 j++;
           }      
          }  
        
		for (int p=1; p<=m; p++)
            
        {  int k=0;
           for (int n=0; n<vector1.size(); n++)
           { 
           String bez = "bez" ;
              if((vector1.get(n)).get(0).equals(bez))
               { k++;  int j=0;
                while((vector1.get(n)).size()>j)
                 { j++; }
                (vector1.get(n)).remove(j-1);
                (vector1.get(n)).add(jumpaddr[k]);
                 ht.put((vector1.get(n)).get(j-1),(vector1.get(n)).get(j-1));
                                  }
               
           }
        }
		
		 for(int n=0; n<vector1.size(); n++)
	        { int j=0;
	           String load = "load" ;
	           String store = "store" ; 
	           if((vector1.get(n)).get(0).equals(load)||(vector1.get(n)).get(0).equals(store))            
	            {  while((vector1.get(n)).size()>j)      
	                  { j++; }    
	              for(int r=0; r<(vector1.get(n)).get(j-1).length();r++ )
	              {  
	                if((vector1.get(n)).get(j-1).charAt(r)=='(')
	                 {  String no = (vector1.get(n)).get(j-1).substring(0,r);
	                    System.out.println("the value of no"+ no);
	                    String reg = (vector1.get(n)).get(j-1).substring(r+1,((vector1.get(n)).get(j-1).length())-1) ;
	                    (vector1.get(n)).remove(j-1);
	                    (vector1.get(n)).add(reg);
	                    System.out.println("the length of the string" + (vector1.get(n)).toString().length());
	                    System.out.println("vector 1 line = " +(vector1.get(n)).toString() );
	                    System.out.println("the value of reg "  +reg);
	                      int temp = Integer.parseInt(no); 
	                      String offset = Integer.toBinaryString(temp);
	                      offset = fillString('0',16-offset.length()) + offset ;
	                      (vector1.get(n)).add(offset);
	                      ht.put((vector1.get(n)).get(j),(vector1.get(n)).get(j));
	                  }
	               }  
	             }
	          }  
		 
		  for(int n=0; n<vector1.size(); n++)
	        {   int q = 0; 
	     	   String addi = "addi" ;
	        if((vector1.get(n)).get(0).equals(addi) || (vector1.get(n)).get(1).equals(addi))
	        { while((vector1.get(n)).size()>q)      
	             { q++; } 
	        String noimm = (vector1.get(n)).get(q-1) ; 
	        int temp = Integer.parseInt(noimm);
	        String immvalue = Integer.toBinaryString(temp);
	        System.out.println("the value of immidiate value"+ immvalue);   
	        immvalue = fillString('0',16-immvalue.length()) + immvalue ;
	        (vector1.get(n)).remove(q-1);
	        (vector1.get(n)).add(immvalue);
	         ht.put((vector1.get(n)).get(q-1),(vector1.get(n)).get(q-1));
	        }
	        
	        }
		  
		  for(int n=0; n<vector1.size(); n++)
	        { int j=0;
	          while((vector1.get(n)).size()>j)
	           {  System.out.println("Final[" + n +"][" +j+ "]" +   ht.get((vector1.get(n)).get(j)));
	               j++;
	           }
	        }
		  
		  for(int n=0; n<vector1.size(); n++)
	         { int j=0;
	           String line = "";
	           String k ="";
	           while((vector1.get(n)).size()>j)
	            {      
	              if(ht.get((vector1.get(n)).get(j))!=null)
	                k = ht.get((vector1.get(n)).get(j)) ;
	               else
	               k="";
	                
	           line =  line + k;
	                j++;
	            }      
	                
	              
	              line = n + " " + line + fillString('0',32-line.length()) + "  " + vector2.get(n) ;
	              out.write(line);
                  out.newLine();
	          
	            }
		  out.close();
		

	}

	
}